//导入请求的域名
import { baseUrl } from './http.js'
 
 
module.exports = {
  request : function(url, methodType, data){
    let fullUrl = `${baseUrl}${url}`
    
    wx.showLoading({
      title: "请稍候...",
      mask: true //遮蔽层
    });
    return new Promise((resolve,reject)=>{
      wx.request({
        url: fullUrl,
        method:methodType,
        data,
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization':wx.getStorageSync('token')||''
        },
        success(res){
         
          if (res.data.status == 200 || res.data.status == 204 ) {
            wx.hideLoading()
            resolve(res.data)
          }else{
            wx.hideLoading({
                complete(){
                    resolve(res.data)
                },
            }  
            )
          }
        },
        fail(err){
          console.log(err)
          var msg='网络错误,请重试';
          if(err.errno===5||err.errMsg.indexOf('time out')!=-1){
            msg="网络超时,请重试"
          }
          wx.showToast({
            title: msg,
            icon:'none'
          })
          reject(msg)
        }
      })
    })
  }
}